﻿<?php
include("config.php");
$languageId = 32;
session_start();
$_SESSION['username'] = 'tester';
if(!isset($_SESSION['username']))
	header('location:http://shop.autohit.bg/');
$username = $_SESSION['username'];
if(isset($_GET['engineId']))
	$vehicle = $_SESSION['vehicle'];

if(isset($_SESSION['admin_user_id']))
{
	$admin = $_SESSION['admin_user_id'];
	$admin_username = $_SESSION['admin_username'];
}
$_SESSION = array();

if(isset($admin))
{
	$_SESSION['admin_user_id'] = $admin;
	$_SESSION['admin_username'] = $admin_username;
}

$_SESSION['username'] = $username;
if(isset($_GET['engineId']))
{
	$_SESSION['engine_id'] = $_GET['engineId'];
	$_SESSION['engine_code'] = $_GET['engine_code'];
	$_SESSION['vehicle'] = $vehicle;
}
error_reporting(1);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="bg" lang="bg">
<head>
	<title>Вътрешна система за autohit</title>
	<meta http-equiv="content-type" content="text/html; charset=Windows-1251" />

	<!-- Scripts -->
	<script type='text/javascript' src='js/scripts.js'></script>

	<!-- Style -->
	<link rel="stylesheet" type="text/css" href="css/styles.css" />
	
		<!-- =========================== Fancy Effect =================================== -->
	<!-- Add jQuery library -->
	<script type="text/javascript" src="fancybox/lib/jquery-1.10.1.min.js"></script>

	<!-- Add mousewheel plugin (this is optional) -->
	<script type="text/javascript" src="fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>

	<!-- Add fancyBox main JS and CSS files -->
	<script type="text/javascript" src="fancybox/source/jquery.fancybox.js?v=2.1.5"></script>
	<link rel="stylesheet" type="text/css" href="fancybox/source/jquery.fancybox.css?v=2.1.5" media="screen" />

	<!-- Add Button helper (this is optional) -->
	<link rel="stylesheet" type="text/css" href="fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.5" />
	<script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.5"></script>

	<!-- Add Thumbnail helper (this is optional) -->
	<link rel="stylesheet" type="text/css" href="fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.7" />
	<script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.7"></script>

	<!-- Add Media helper (this is optional) -->
	<script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.6"></script>

	<script type="text/javascript">
		function iframeInfoBox(id){
			$.fancybox.open({
					href : 'iframe.php?id='+id,
					type : 'iframe',
					padding : 5
				});
		}
	</script>
</head>
<body onLoad="document.getElementById('iframe_result').style.display='none';">
<?php
/**
	MANUFACTURY
*/
//default param
$languageId = 32;

$options = array('languageId'=>$languageId,'trace' => 1,'login' => 'autohit2','password' => 'bont39er','encoding' => 'UTF-8','trace' => true,'exceptions' => true);
$client = new SoapClient('http://autohit.web-catalogue.eu/main/service/vehicle.wsdl',$options);

//manufacturers
$parameters_manufacturer = array('languageId' => $languageId, 'hidden' => 0, 'pc' => 1, 'cv' => 0, 'all' => 1);
$result_manufacturer =  $client->searchVehicleManufacturers($parameters_manufacturer);

// Производители
$query = sprintf("SELECT `manufacturer_id`, `name` FROM `manufacturers` WHERE `deleted` = 0");
$results = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($results);

$arrManufacturerTableMy = array();
if($num_rows){
	while($fetch = mysql_fetch_assoc($results)){
	$arrManufacturerTableMy[$fetch['manufacturer_id']] = $fetch['name'];
	}
}

$arrManufacturerAll = array();		// Всички производители който идват от wsdl файла
$arrManufacturerOther = array();	// Производители които не присъстват в таблица << manufacturers >>
$arrManufacturerMy = array();		// Всички производители от таблица << manufacturers >>
if($result_manufacturer && count($result_manufacturer))
{
  foreach ($result_manufacturer->manufacturer as $key => $data)
  {
	if(in_array($data->name, $arrManufacturerTableMy))
	  $arrManufacturerMy[$data->id] = $data->name;
	else
	  $arrManufacturerOther[$data->id] = $data->name;

	$arrManufacturerAll[$data->id] = str_replace('Ë', '&#203;', $data->name);
  }


/*
// Engine code
$parameters_engine = array('languageId'=>$languageId,'manufacturerId'=>$_GET['manufacturerId'],'engineId'=>$_GET['engineId'],'pc'=>1,'cv'=>0,'modelFilter'=>1);
$result_engine = $client->searchVehicleModelSeries($parameters_engine);
echo "<br/>aaa";
print_r($result_engine);

if (is_object($result_engine->modelSeries) && (count(get_object_vars($result_engine->modelSeries)) > 0)) {
  echo "IN";
}
else
  echo "OUT";
exit;


	if(!is_array($result_engine->modelSeries))
		$result_engine->modelSeries = array($result_engine->modelSeries);

	if(count($result_engine->modelSeries)>0)
	      echo "aaaaaa";
	else
	      echo "bbb";

 exit;
*/
?>
<div id ="test"></div>
	<div class="topHeader">
		<div class="topHeaderSel">
			<span style="font-size: 14px; font-weight: bold;">Производител</span><br/>
			<select class="select" name="manufacturer" id="manufacturer" onchange="ajaxModelsSeries(1,this.value, 0, <?php echo ((isset($_GET['engineId']) && (!empty($_GET['engineId']))) ? $_GET['engineId'] : 0) ?>);ajaxManufacturerLoadModels(1, this.value);">
<?php
			if(!isset($_GET['manufacturerId'])){
?>
				<option value="0">- Моля изберете производител -</option>
<?php
			}

			foreach ($arrManufacturerAll as $id => $name){
				if(isset($_GET['manufacturerId'])){
					if($_GET['manufacturerId']==$id){
?>
						<option id="manufacturer[<?php echo $id;?>]" value="<?php echo $id;?>" selected> <?php echo $name;?> </option>
<?php
					}
				}
				else{
?>
						<option id="manufacturer[<?php echo $id;?>]" value="<?php echo $id;?>"> <?php echo $name;?> </option>
<?php
				}
			 }

			 $_SESSION['manufacturers'] = $arrManufacturerAll;
?>
			</select>
		</div>
		<div class="topHeaderSeperator">&nbsp;</div>
		<div class="topHeaderSel">
			<span style="font-size: 14px; font-weight: bold;">Сериен модел</span><br/>
			<select class="select" id="model_series" name="model_series" onchange="var manufacturerct_id = document.getElementById('manufacturer'); ajaxShowResults(2, manufacturerct_id.options[manufacturerct_id.selectedIndex].value, this.value, <?php echo (isset($_GET['engineId']) ? $_GET['engineId'] : 0) ?>)">
				<option value="0">- Моля изберете сериен модел -</option>
				<?php
					if(isset($_GET['manufacturerId']) && isset($_GET['engineId'])){
						// Engine code
						$parameters_engine = array('languageId'=>$languageId,'manufacturerId'=>$_GET['manufacturerId'],'engineId'=>$_GET['engineId'],'pc'=>1,'cv'=>0,'modelFilter'=>1);
						$result_engine = $client->searchVehicleModelSeries($parameters_engine);

						if(!is_array($result_engine->modelSeries))
							$result_engine->modelSeries = array($result_engine->modelSeries);
						$result_engine->modelSeries = array_filter($result_engine->modelSeries);
						
						if (isset($result_engine->modelSeries)) {
							foreach($result_engine->modelSeries as $key=>$data){
								if(isset($data->manufacturedFrom))
									$manufacturedFrom = trim($data->manufacturedFrom);
								else
									$manufacturedFrom = '';
								
								if(isset($data->manufacturedTo))
									$manufacturedTo = trim($data->manufacturedTo);
								else
									$manufacturedTo = '';
		
								$optionId = $data->id;
								$optionName = $data->name.' ('.$manufacturedFrom.' - '.$manufacturedTo.')';
				?>
								<option value="<?php echo $optionId;?>"> <?php echo $optionName;?> </option>
				<?php
							}
						}
					}
				?>
			</select>
		</div>
		<div class="topHeaderSeperator">&nbsp;</div>
		<div class="topHeaderInp">
			<span style="font-size: 14px; font-weight: bold;">Код на мотор</span><br/>
			<input type="text" name="code_engine" id="code_engine" class="input" maxlength="200" style="height: 20px;" onkeypress="return submitViaEnter(event, 3, document.getElementById('code_engine').value)" value="<?php echo trim(urldecode($_GET['engine_code'])); ?>" />
		</div>
		<div class="topHeaderBtn">
			<br/><input type="button" name="btn" id="btn" class="btn" onClick="ajaxEngineCode(3, document.getElementById('code_engine').value)" onmouseover="this.style.cursor='pointer';" onmouseout="this.style.cursor='normal';"  value="Търси" />
		</div>
		<div class="topHeaderHistoryBtn"><br/>
			<select class="select" id="model_history" name="model_history" onChange="loadHistory(this.value)">
				<option value="0">- История на автомобили -</option>
			<?php
				// History
// 				$query = sprintf("SELECT `user_history_id`, `manufacturer_id`, `model_seria_id`, `product_group_id`, `label`, `created`
// 						  FROM `users_history`
// 						  WHERE `user_id` = ".$_SESSION['user_id']."
// 						  ORDER BY `created` DESC
// 						  LIMIT 0, 10");
				$query = sprintf("SELECT `user_history_id`, `manufacturer_id`, `model_seria_id`, `product_group_id`, `label`, `created`
						  FROM `users_history`
						  WHERE username = '".$_SESSION['username']."'
						  ORDER BY `created` DESC
						  LIMIT 0, 10");
				$results = mysql_query($query) or die(mysql_error());
				$num_rows = mysql_num_rows($results);

				if($num_rows){
					while($fetch = mysql_fetch_assoc($results)){
			?>
						<option value="<?php echo $fetch['user_history_id'].'_'.$fetch['manufacturer_id'].'_'.$fetch['model_seria_id'].'_'.$fetch['product_group_id']; ?>"> <?php echo $fetch['label']; ?></option>
			<?php
					}
				}
			?>
			</select>
		</div>
	</div>
	<div class="clear"></div><br/>

	<?php
		if( (!isset($_GET['manufacturerId'])) && (!isset($_GET['engineId'])) ){
	?>
			<div id="main_car_manufacturer">
				<a href="index.php">
					<div id="leki_avtomobili" style="float: left; width: 123px; height: 15px; margin-bottom:5px;" onmouseover='this.style.cursor="pointer";' onmouseout='this.style.cursor="normal";'></div>
				</a>
				<div id="manufacturer_all" style="float: left; width: 100%; display: none;">
					<div class="car_manufacturer" style="float: left;">Лек автомобил</div>
					<?php
						// Показване ВСИЧКИ производители
						$arrayManufacturer = array();

						foreach ($arrManufacturerAll as $id => $name){
							$arrayManufacturer[$id] = "<div class='car_manufacturer_left' id='table_manufacturer[".$id."]'>
											<a onmouseover='mouseOver(this);' onmouseout='mouseOut(this);' onClick=\"this.style.color='#ff0000'; ajaxManufacturerLoadModels(1, ".$id.");\">
											  <b>".$name."</b>
											</a>
										    </div>";
						}

						if(count($arrayManufacturer)>0){
							$count = 2;
							$arrOut = array_chunk($arrayManufacturer, ceil(count($arrayManufacturer)/$count));
							array_unshift($arrOut, null);
							$arrOut = call_user_func_array("array_map", $arrOut);

							foreach ($arrOut as $br => $row){
								foreach($row as $index => $name){
									echo $name;
								}
							}
						}
					?>

					<div style="float: left; margin-top: 10px; width: 100%;" onClick="document.getElementById('manufacturer_all').style.display = 'none'; document.getElementById('manufacturer_my').style.display = 'block';">
						<img src="images/arrow_right.png" alt="" style="float: left; margin-right: 7px;" />
						<span onmouseover="this.style.cursor='pointer'; this.style.color='#ff0000'; this.style.textDecoration = 'none';" onmouseout="this.style.cursor='normal'; this.style.color='#000000'; this.style.textDecoration = 'underline';" style="float: left;font-size: 13px; text-decoration: underline;"><b>Първа страница производители</b></span>
					</div>
				</div>

				<div id="manufacturer_my" style="float: left; width: 100%; display: block;">
					<div class="car_manufacturer" style="float: left;">Лек автомобил</div>
					<?php
						// Показване САМО избрани производители
						$arrayManufacturer = array();

						foreach ($arrManufacturerMy as $id => $name){
							$arrayManufacturer[$id] = "<div class='car_manufacturer_left' id='table_manufacturer[".$id."]'>
											  <a onmouseover='mouseOver(this);' onmouseout='mouseOut(this);' onClick=\"this.style.color='#ff0000'; ajaxManufacturerLoadModels(1, ".$id.");\">
											      <b>".$name."</b>
											  </a>
										   </div>";
						}

						if(count($arrayManufacturer)>0){
							$count = 2;
							$arrOut = array_chunk($arrayManufacturer, ceil(count($arrayManufacturer)/$count));
							array_unshift($arrOut, null);
							$arrOut = call_user_func_array("array_map", $arrOut);

							foreach ($arrOut as $br => $row){
								foreach($row as $index => $name){
									echo $name;

								}
							}
						}
					?>
					<div style="float: left; margin-top: 10px; width: 100%;" onClick="document.getElementById('manufacturer_all').style.display = 'block'; document.getElementById('manufacturer_my').style.display = 'none';">
						<img src="images/arrow_right.png" alt="" style="float: left; margin-right: 7px;" />
						<span onmouseover="this.style.cursor='pointer'; this.style.color='#ff0000'; this.style.textDecoration = 'none';" onmouseout="this.style.cursor='normal'; this.style.color='#000000'; this.style.textDecoration = 'underline';" style="float: left;font-size: 13px; text-decoration: underline;"><b>Други производители</b></span>
					</div>
				</div>
			</div>
			<div class="clear"></div>
	<?php
		}
		elseif(isset($_GET['manufacturerId']) && isset($_GET['engineId'])){
			// Engine code
			$parameters_engine = array(
				'languageId' => $languageId,
				'manufacturerId' => $_GET['manufacturerId'],
				'engineId' => $_GET['engineId'],
				'pc' => 1,
				'cv' => 0,
				'modelFilter' => 1
			);
			$result_engine = $client->searchVehicleModelSeries($parameters_engine);

			if(!is_array($result_engine->modelSeries))
				$result_engine->modelSeries = array($result_engine->modelSeries);

	?>
			<div id="main_car_manufacturer">
				<div style="float:left; width: 100%; margin-bottom: 5px; font-size: 14px; font-weight: bold;">

					<a href="index.php">
						<div id="leki_avtomobili" style="float: left; width: 123px; height: 15px; margin-bottom:5px;" onmouseover='this.style.cursor="pointer";' onmouseout='this.style.cursor="normal";'></div>
					</a>
					<div style="float: left;">&nbsp;&gt;&gt;&nbsp;</div>
					<div style="float: left;">
	<?php
						echo (isset($_GET['manufacturerId']) && isset($_SESSION['manufacturers'][$_GET['manufacturerId']])) ?  $_SESSION['manufacturers'][$_GET['manufacturerId']] : ''
	?>
					</div>
				</div>
				<div class="car_manufacturer" style="float: left;">
				    <div class="car_manufacturer_left">
					    <div style="float: left;"><img src="images/modelna_poredica.png" alt="" /></div>
					    <div style="float: right;"><img src="images/bql_godina_na_proizvodstvo.png" alt="" /></div>
				    </div>
				    <div class="car_manufacturer_right">
					    <div style="float: left;"><img src="images/modelna_poredica.png" alt="" /></div>
					    <div style="float: right;"><img src="images/bql_godina_na_proizvodstvo.png" alt="" /></div>
				    </div>
				</div>
	<?php
			$i = 0;
			$_SESSION['model_series'] = array();
			$arrayModelSeries = array();

			if(!is_array($result_engine->modelSeries))
				$result_engine->modelSeries = array($result_engine->modelSeries);
			$result_engine->modelSeries = array_filter($result_engine->modelSeries);

			if (isset($result_engine->modelSeries)) {
				foreach($result_engine->modelSeries as $key=>$data){
					$_SESSION['model_series'][$data->id] = $data->name;
					if(isset($data->manufacturedFrom)){
						if(substr(trim($data->manufacturedFrom), 3, -1)==0)
						      $manufacturedFrom = '20'.substr(trim($data->manufacturedFrom), 3, 4);
						else
						      $manufacturedFrom = '19'.substr(trim($data->manufacturedFrom), 3, 4);
					}
					else
						$manufacturedFrom = '';
								
					if(isset($data->manufacturedTo)){
						if(substr(trim($data->manufacturedTo), 3, -1)==0)
						      $manufacturedTo = ' - 20'.substr(trim($data->manufacturedTo), 3, 4);
						else
						      $manufacturedTo = ' - 19'.substr(trim($data->manufacturedTo), 3, 4);
					}
					else
						$manufacturedTo = '';

					$i++;
					//$bgcolor = ( ($i % 2 ==0) ? "#ffffff" : "#eeeeee");

					$id = $data->id;
					$name = $data->name;

					$arrayModelSeries[$id] = '<div class="car_manufacturer_left" id="table_manufacturer['.$id.']">
									<a onmouseover="mouseOver(this);" onmouseout="mouseOut(this);" onClick="var manufacturerct_id = document.getElementById(\'manufacturer\'); ajaxShowResults(2, manufacturerct_id.options[manufacturerct_id.selectedIndex].value, '.$id.', '.(isset($_GET['engineId']) ? $_GET['engineId'] : 0).')">
										<span style="font-weight: bold;">'.$name.'</span>
									</a>
									<div style="float: right;" onClick="var manufacturerct_id = document.getElementById(\'manufacturer\'); ajaxShowResults(2, manufacturerct_id.options[manufacturerct_id.selectedIndex].value, '.$id.', '.(isset($_GET['engineId']) ? $_GET['engineId'] : 0).')"><b>'.$manufacturedFrom.$manufacturedTo.'</b></div>
								  </div>';
				}

				// Count
				if(count($arrayModelSeries)>0)
				{
					if(count($arrayModelSeries)==1){
					      foreach ($arrayModelSeries as $value){
						    echo $value;
					      }
					}
					else{
					      $count = 2;
					      $arrOut = array();
					      $arrOut = array_chunk($arrayModelSeries, ceil(count($arrayModelSeries)/$count));
					      array_unshift($arrOut, null);
					      $arrOut = call_user_func_array("array_map", $arrOut);

					      foreach ($arrOut as $br => $row){
						      foreach($row as $index => $name){
							      echo $name;
						      }
					      }
					}
				}
				else
					echo '<div class="no_result">Няма намерени резултати.</div>';
			}
			else
				echo '<div class="no_result">Няма намерени резултати.</div>';
	?>
		</div>
	<?php
		}
		else{
	?>
			<div id="main_car_manufacturer"></div>
	<?php
		}
	?>

	<div id="divIFrame"></div>
	<!-- IFRAME -->
	<iframe id="iframe_result" onLoad="document.getElementById('divIFrame').innerHTML = '';" width="100%" height="800" style="border: hidden; overflow: auto;"></iframe>
<?php
}
else
	echo 'Няма намерени резултати.';
?>
</body>
</html>
